<?php
namespace App\Services\EnterpriseAccount;
use App\Model\Enterprise;
use App\Services\CommonService;
use App\Model\EnterpriseAccount;
class EnterpriseAccountService extends CommonService
{
    public function UserLogin($data)
    {

        $res=EnterpriseAccount::where("u_phone","=",$data['phone'])
            ->get()
            ->toArray();

        if($res[0]['u_password']!==$data['password']){
            return $this->fail(213,'密码不正确');
        }

        $token=md5($res[0]['id'].$res[0]['u_phone'].$res[0]['u_password'].time());
        unset($res[0]['u_password']);
        $redis=new \redis();//实例化redis
        $redis->connect("127.0.0.1",6379);//连接token
        if($tokenName=$redis->get($data['phone']))  //看看用户有没有token
        {
            $redis->delete($tokenName);   //删除以前的token
        }
        $Enterproce=Enterprise::where('id','=',$res[0]['id'])->get()->toArray();
        $Enterproce[0]['u_phone']=$data['phone'];
        if($Enterproce===array())
        {
            return $this->fail(301,'查询企业失败');
        }

        $result=$redis->set($token,json_encode($Enterproce),25200); //设置token
        $isok=$redis->set($data['phone'],$token,25200);
        if($result&&$isok)
        {
            $array['token']=$token;  //1是企业
            $array['type']=1 ;
            return $this->success($array);
        }
        else
        {
            $redis->delete($token,$data['phone']);
            return  $this->fail(214,'设置token失败');
        }
    }
}